//
//  ConOrReListViewController.m
//  LuxuryGoods
//
//  Created by DCQ on 2018/5/22.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "ConOrReListViewController.h"
#import "OrderGoodsCell.h"
#import "UserInfoModel.h"
#import "OrderListModel.h"
#import "LGOrderSectionCell.h"
#import "LGOrderGoodsCell.h"
#import "LGOrderBottomCell.h"
#import "LGOrderMultipleCell.h"
#import "AddSendInfoViewController.h"
#import "OrderLogisticsViewController.h"
#import "ConsignmentOrRecyclingDetailVC.h"


NSString * const ConOrReListSectionCellIdentifier = @"ConOrReListSectionCellIdentifier";
NSString * const ConOrReListGoodsCellIdentifier = @"ConOrReListGoodsCellIdentifier";
NSString * const ConOrReListMultipleCellIdentifier = @"ConOrReListMultipleCellIdentifier";
NSString * const ConOrReListBottomCellIdentifier = @"ConOrReListBottomCellIdentifier";
@class XLPagerTabStripViewController;
@interface ConOrReListViewController ()
@property(nonatomic,assign) ConOrReTableViewControllerStyle style;
@property(nonatomic,assign) ConOrReVCType type;
@property(nonatomic,strong) OrderListModel * listModel;
@end

@implementation ConOrReListViewController
- (instancetype)initWithStyle:(ConOrReTableViewControllerStyle)style withType:(ConOrReVCType)type{
    self = [super init];
    if (self) {
        self.style = style;
        self.type = type;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNormalTableViewNetworkData) name:@"UPDATE_ConOrReOrder_LIST" object:nil];
 
}
- (void)updateList:(id)sender{
    [self requestOrderList];
}
- (void)requestOrderList{
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params addEntriesFromDictionary:@{@"token":UserInfo.userToken}];
    switch (self.style) {
        case ConOrReTableViewControllerStyle_all:
            
            break;
        case ConOrReTableViewControllerStyle_waitOffer:
            [params addEntriesFromDictionary:@{@"status":@"0"}];
            break;
        case ConOrReTableViewControllerStyle_alreadyOffer:
            [params addEntriesFromDictionary:@{@"status":@"10"}];
            break;
        case ConOrReTableViewControllerStyle_alreadySend:
            [params addEntriesFromDictionary:@{@"status":@"20"}];
            break;
        case ConOrReTableViewControllerStyle_sell:
            [params addEntriesFromDictionary:@{@"status":@"30"}];
            break;
            
        case ConOrReTableViewControllerStyle_complete:
            [params addEntriesFromDictionary:@{@"status":@"40"}];
            break;
   
            
        default:
            break;
    }
    NSString * order_type = @"";
    if (self.type == ConOrReVCType_consignment) {
        order_type = @"1";
    }else if (self.type == ConOrReVCType_recycling){
        order_type = @"2";
    }
    [params addEntriesFromDictionary:@{@"order_type":order_type}];
    [params addEntriesFromDictionary:@{@"p":@(self.currentPageNo)}];
    [params addEntriesFromDictionary:@{@"psize":@(self.pageCount)}];
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Consignorder/getList" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        NSLog(@"%@===",responseObject);
        if (responseObject && !error) {
            if ( strongSelf.currentPageNo == 0) {
                strongSelf.listModel = nil;
                strongSelf.listModel = [[OrderListModel alloc]initWithDictionary:responseObject error:nil];
                strongSelf.currentPageNo ++;
            }else{
                if (responseObject[@"data"] && [responseObject[@"data"] count] > 0 ) {
                    [strongSelf.listModel.data addObjectsFromArray: responseObject[@"data"]];
                    strongSelf.currentPageNo ++;
                }
            }
            
        }
        [strongSelf updateTableView];
        
    }];
    
}
- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LGOrderSectionCell class]) bundle:nil] forCellReuseIdentifier:ConOrReListSectionCellIdentifier];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LGOrderGoodsCell class]) bundle:nil] forCellReuseIdentifier:ConOrReListGoodsCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LGOrderMultipleCell class]) bundle:nil] forCellReuseIdentifier:ConOrReListMultipleCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LGOrderBottomCell class]) bundle:nil] forCellReuseIdentifier:ConOrReListBottomCellIdentifier];
}
- (CGRect)getTableViewFrame{
    CGRect tableViewFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 44);
    return tableViewFrame;
}
- (BOOL)isAddRefreshHeader{
    return YES;
}
- (BOOL)isAddRefreshFooter{
    return YES;
}

- (NSInteger )getNetworkTableViewDataCount{
    
    return  [self.listModel.data count];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger section = 0;
    section = [self.listModel.data count];
    return section;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rows = 2;
    OrderModel * model = self.listModel.data[section];
    //0-待报价 10-已报价 20-已寄件 30-出售中(寄卖专用) 40-已完成 90-已取消
    if ([model.status integerValue] == 10 ||[model.status integerValue] == 20) {
        rows = 3;
    }
    return rows;
}
- (NSString *)titleForPagerTabStripViewController:(XLPagerTabStripViewController *)pagerTabStripViewController{
    NSString * tabTitle = @"";
    switch (self.style) {
        case ConOrReTableViewControllerStyle_all:
            tabTitle = @"全部";
            break;
        case ConOrReTableViewControllerStyle_waitOffer:
            tabTitle = @"待报价";
            break;
        case ConOrReTableViewControllerStyle_alreadyOffer:
            tabTitle = @"已报价";
            break;
        case ConOrReTableViewControllerStyle_alreadySend:
            tabTitle = @"已寄件";
            break;
        case ConOrReTableViewControllerStyle_sell:
            tabTitle = @"出售中";
            break;
        case ConOrReTableViewControllerStyle_complete:
            tabTitle = @"已完成";
            break;
        case ConOrReTableViewControllerStyle_cancel:
            tabTitle = @"已取消";
            break;
        default:
            break;
    }
    return   tabTitle;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat  height = 0;
    if (indexPath.row == 0) {
        height = 50;
    }else if (indexPath.row == 1){
        height = 70;
    }else if (indexPath.row == 2){
        height = 50;
    }
    return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    OrderModel * model = self.listModel.data[indexPath.section];
    if (indexPath.row == 0) {
        LGOrderSectionCell * tempCell = [tableView dequeueReusableCellWithIdentifier:ConOrReListSectionCellIdentifier];
        ViewSectionType type =  ViewSectionType_normal;
        if (self.type == ConOrReVCType_recycling) {
            type = ViewSectionType_Re;
        }
        else if (self.type == ConOrReVCType_consignment) {
            type = ViewSectionType_Con;
        }
        [tempCell setupOrderData:model withType:type];
        cell = tempCell;
    }else if(indexPath.row == 1){
        
        if ([model.goods_item count] >1) {
            LGOrderMultipleCell * tempCell = [tableView dequeueReusableCellWithIdentifier:ConOrReListGoodsCellIdentifier];
            [tempCell setupOrderData:model];
            cell = tempCell;
        }else{
            LGOrderGoodsCell * tempCell = [tableView dequeueReusableCellWithIdentifier:ConOrReListMultipleCellIdentifier];
            [tempCell setupOrderData:model];
            cell = tempCell;
        }
        
    }else if (indexPath.row == 2){
        LGOrderBottomCell * tempCell = [tableView dequeueReusableCellWithIdentifier:ConOrReListBottomCellIdentifier];
        ViewType type = ViewType_normal;
        if (self.type == ConOrReVCType_recycling) {
           type = ViewType_Re ;
        }else if (self.type == ConOrReVCType_consignment){
            type = ViewType_Con ;
        }
        
        [tempCell setupOrderData:model withViewType:type];
        tempCell.btnBlock = ^(OrderBottomStyle style, NSString *orderId) {
            switch (style) {
                case OrderBottomStyle_completeSend:
                      [self gotoLogisticsVC:orderId];
 
                    break;
                case OrderBottomStyle_completeOffer:
 
                    break;
 
                default:
                    break;
            }
        };
        cell = tempCell;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * orderId = @"";
    OrderModel * model = self.listModel.data[indexPath.section];
    NSInteger  status  = [model.status integerValue];
    ConsignmentOrRecyclingDetailType type = ConsignmentOrRecyclingDetailType_normal;
    if (self.type == ConOrReVCType_recycling) {
        type =  ConsignmentOrRecyclingDetailType_Re;
    }else if (self.type == ConOrReVCType_consignment){
        type = ConsignmentOrRecyclingDetailType_Con;
    }

    orderId =[NSString stringWithFormat:@"%@",model.order_id];
    ConsignmentOrRecyclingDetailVC * detailVC = [[ConsignmentOrRecyclingDetailVC alloc] initWithStyle:status withVCType:type withOrderId:orderId];
    [self pushViewController: detailVC];
    
    
}

#pragma mark ----
//   页面加载调用 子类重写
- (void)getNormalTableViewNetworkData{
    NSLog(@"进入页面请求 子类重写");
    self.currentPageNo = 0;
    self.pageCount = 10;
 
    [self requestOrderList];
}
- (void)getLoadMoreTableViewNetworkData{
    
    [self requestOrderList];
}
#pragma mark ---

- (void)gotoLogisticsVC:(NSString *) orderId{
    OrderLogisticsViewController * logisticsVC = [[OrderLogisticsViewController alloc]initOrderId:orderId withVCType:LogisticsVCType_conOrRe ];
    [self pushViewController:logisticsVC];
    
}
- (void)gotoAddSendInfo:(NSString *)orderId{
    
    AddSendInfoViewController * sendInfoVC = [[AddSendInfoViewController alloc]initWithOrderId:orderId withType:AddSendInfoVCType_conOrRe];
    [self pushViewController:sendInfoVC];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
